//app.js
import getIn from './sdk/utils/getIn';
import request from './sdk/utils/request.js';
import navigateHelper from './sdk/utils/navigateHelper.js';
import { notify } from './sdk/utils/NotifyRegistryCenter.js';
import { API_MEMBER_AUTH } from './sdk/business/url';

App({
    onLaunch: function () {
        global.request = request;
        global.navigateHelper = navigateHelper;
        wx.getAppBaseInfo({
          success(res) {
            const version = res.SDKVersion.replace(/\./g, '');
            if (parseInt(version, 10) < 170) {
              wx.showModal({
                content: '当前微信版本过低,为了正常使用请先升级再使用',
              });
              return;
            }
          },
          fail(e){
            console.log("e",e)
          }
        });
        setTimeout(()=>{
            if (!this.globalData.token) {
                this.login()
            }
        }, 5000)
        
    },
    // 默认登录
    login: function (param={}, callback) {
        wx.login({
            success(res) {
                const code = res && res.code;
                const params = {
                    code: code,
                    ...param
                };
                request(API_MEMBER_AUTH, params, (status, result) => {
                    if (status == 'SUCCESS') {
                        wx.setStorageSync('token', getIn(result, ['data', 'token']));
                        getApp().globalData.token = getIn(result, ['data', 'token']);
                        const storageUserInfo = wx.getStorageSync('userInfo');
                        let userInfo = getIn(result, ['data']);
                        if (storageUserInfo && storageUserInfo.head_photo && !param.iv) {
                            wx.setStorageSync('userInfo', { ...userInfo, head_photo: storageUserInfo.head_photo, nickname: storageUserInfo.nickname });
                            getApp().globalData.userInfo = { ...userInfo, head_photo: storageUserInfo.head_photo, nickname: storageUserInfo.nickname };
                        } else {
                            wx.setStorageSync('userInfo', userInfo);
                            getApp().globalData.userInfo = userInfo;
                        }
                        notify('LOGIN_NOTI', getIn(result, ['data', 'token']));
                        callback && callback('SUCCESS', { token: getIn(result, ['data', 'token']), userInfo });
                    } else if (status == 'FAIL') {
                      callback && callback('FAIL', { token: '', msg: getIn(result, ['msg']) });
                    }
                });
            }
        });
    },
    // 授权登陆
    wxLogin(userInfo={}, callback) {
        const { iv } = userInfo;
        const { encryptedData } = userInfo;
        const param = { iv, encryptedData };
        this.login(param, callback);
    },
    // 检测token值
    userInfo () {
        return new Promise((resolve, reject) => {
            if (!this.globalData.token) {
                this.login({}, (code, res) => {
                    if (code == 'SUCCESS') {
                      resolve && resolve(res)
                    } else {
                        reject && reject();
                    }
                });
            } else {
              resolve && resolve({ token: this.globalData.token, userInfo: this.globalData.userInfo})
            }
        });
    },
    globalData: {
        userInfo: null
    }
})